//
//  WebViewController.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/10/11.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIView *downView;;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, strong) NSDictionary *data;

@end

@implementation WebViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setUpWebView];
    
    if (!self.isLoadHtml)
    {
        [self setNav];
    }
}

- (void)setNav
{
    UIImageView *rightIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"w_share"]];
    [self.bgNavView addSubview:rightIcon];
    
    UILabel *rightLabel = [[UILabel alloc] init];
    [self.bgNavView addSubview:rightLabel];
    rightLabel.text = @"分享";
    rightLabel.textAlignment = 1;
    rightLabel.textColor = RGBACOLOR(52, 52, 52, 1);
    rightLabel.font = FontWithSize(14);
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.bgNavView addSubview:rightBtn];
    [rightBtn addTarget:self action:@selector(sharkBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    if (@available(iOS 11.0, *))
    {
        rightIcon.frame = CGRectMake(SCREENWIDTH-48-16, 35+TopStatusHeight_xs, 16, 16);
        rightLabel.frame = CGRectMake(rightIcon.right+5, 34+TopStatusHeight_xs, 28, 18);
        rightBtn.frame = CGRectMake(SCREENWIDTH-65, 0, 65, self.bgNavView.height);
    } else {
        // Fallback on earlier versions
        rightIcon.frame = CGRectMake(SCREENWIDTH-48-16, 35+TopStatusHeight, 16, 16);
        rightLabel.frame = CGRectMake(rightIcon.right+5, 34+TopStatusHeight, 28, 18);
        rightBtn.frame = CGRectMake(SCREENWIDTH-65, 0, 65, self.bgNavView.height);
    }
}

- (void)sharkBtnClick
{
    NSLog(@"分享");
    [self createDownShareView];
}

- (void)createDownShareView
{
    if (self.downView)
    {
        NSLog(@"存在");
        return;
    }
    //下部view
    self.downView = [[UIView alloc]init];
    [self.view addSubview:self.downView];
    
    if (@available(iOS 11.0, *))
    {
        self.downView.frame = CGRectMake(0, SCREENHEIGHT-130-BottomHeight_xs, SCREENWIDTH, 130+BottomHeight_xs);
    } else {
        // Fallback on earlier versions
        self.downView.frame = CGRectMake(0, SCREENHEIGHT-130-BottomHeight, SCREENWIDTH, 130+BottomHeight);
    }
    self.downView.backgroundColor = [UIColor whiteColor];
    
    for (int i = 0; i < 2; i++)
    {
        CGFloat jiange = (self.downView.width-99-34-34)/2;
        UIImageView *icon = [[UIImageView alloc]init];
        [self.downView addSubview:icon];
        icon.frame = CGRectMake(jiange+(34+99)*(i%2), 17, 34, 34);
        
        UILabel *titleLabel = [[UILabel alloc]init];
        [self.downView addSubview:titleLabel];
        titleLabel.font = FontWithSize(12);
        titleLabel.textColor = BlackTextColor;
        titleLabel.textAlignment = 1;
        titleLabel.center = CGPointMake(icon.centerX, icon.centerY+34);
        titleLabel.bounds = CGRectMake(0, 0, 90, 14);
        
        CGFloat btnJIanGe = (self.downView.width-80-120)/2;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.downView addSubview:button];
        button.frame = CGRectMake(btnJIanGe+(60+80)*(i%2), 15, 60, 60);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i+1;
        
        if (i == 0)
        {
            icon.image = [UIImage imageNamed:@"w_wechat"];
            titleLabel.text = NSLocalizedString(@"微信好友", nil);
        }
        else if (i == 1)
        {
            icon.image = [UIImage imageNamed:@"w_quan"];
            titleLabel.text = NSLocalizedString(@"朋友圈", nil);
        }
        
    }
    
    //分割线
    UIView *lineView = [[UIView alloc]init];
    [self.downView addSubview:lineView];
    lineView.frame = CGRectMake(15, 85, self.downView.width-30, 1);
    lineView.backgroundColor = RGBACOLOR(236, 236, 236, 1);
    
    //取消btn
    UIButton *cancle = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.downView addSubview:cancle];
    if (@available(iOS 11.0, *))
    {
        cancle.frame = CGRectMake(0, self.downView.height-44-BottomHeight_xs, self.downView.width, 44);
    } else {
        // Fallback on earlier versions
        cancle.frame = CGRectMake(0, self.downView.height-44-BottomHeight, self.downView.width, 44);
    }
    
    [cancle setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
    [cancle setTitleColor:BlackTextColor forState:UIControlStateNormal];
    [cancle addTarget:self action:@selector(cancleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)removeDownShareView
{
    [self.downView removeFromSuperview];
    self.downView = nil;
}

#pragma mark - 底部取消按钮绑定事件
- (void)cancleBtnClick:(UIButton *)sender
{
    [self removeDownShareView];
}

- (void)buttonClick:(UIButton *)sender
{
    switch (sender.tag)
    {
        case 1:
            NSLog(@"微信好友");
            [self shareDataWithBtn:sender];
            break;
            
        case 2:
            NSLog(@"朋友圈");
            [self shareDataWithBtn:sender];
            break;
            
        default:
            break;
    }
    [self removeDownShareView];
}

#pragma mark - 整理数据
- (void)shareDataWithBtn:(UIButton *)sender
{
    if ([self.type isEqualToString:@"WXWebpage"])
    {//分享网页
        [self shareHtmlWithType:sender.tag title:self.data[@"title"] descr:self.data[@"desc"] imageUrl:self.data[@"imgUrl"] shareURL:self.data[@"link"]];
    }
    else if ([self.type isEqualToString:@"WXImage"])
    {//分享图片
        NSString *fullImgUrl = self.data[@"fullImgUrl"];
        UIImageView *shareImage = [[UIImageView alloc]init];
        [shareImage sd_setImageWithURL:[NSURL URLWithString:fullImgUrl] placeholderImage:[UIImage imageNamed:@"zhanwei100"]];
        [self sharkImageUMWith:sender.tag withImage:shareImage.image];
    }
}

#pragma mark - 创建webView
- (void)setUpWebView
{
    self.webView = [[UIWebView alloc]init];
    if (@available(iOS 11.0, *)) {
        self.webView.frame = CGRectMake(0, TopBarHeight_xs, SCREENWIDTH, SCREENHEIGHT-TopBarHeight_xs);
    } else {
        // Fallback on earlier versions
        self.webView.frame = CGRectMake(0, TopBarHeight, SCREENWIDTH, SCREENHEIGHT-TopBarHeight);
    }
    [self.view addSubview:self.webView];
    self.webView.delegate = self;
    self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
    self.webView.backgroundColor = [UIColor whiteColor];
    
    NSLog(@"urlString--%@",self.urlString);
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
}

#pragma mark -- webView delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.titLabel.text = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    // 设置javaScriptContext上下文
    self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //将对象指向OC自身
    self.jsContext[@"JsListener"] = self;
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue)
    {
        context.exception = exceptionValue;
        NSLog(@"异常信息：%@", exceptionValue);
    };
}

#pragma mark - JS调用OC方法(分享配置)
- (void)shareMethod:(NSString *)ShareConfig
{
    //1
    NSString *config = [NSString stringWithFormat:@"%@",ShareConfig];
    NSData *jsonData = [config dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    NSLog(@"dic-%@",dic);
    
    //2，接受数据
    self.type = (NSString *)dic[@"type"];
    self.data = (NSDictionary *)dic[@"data"];
    //弹出f底部分享框
    [self createDownShareView];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"code=%li",[error code]);
    if ([error code] == 101) {
        return;
    }
}

@end
